home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11107 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: news.bridge.net!news
  2. From: michael@bridge.net
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How do I read a random I/O file  into a  structure ?
  5. Date: Thu, 21 Mar 1996 23:09:05 GMT
  6. Organization: BridgenetLC - 305.374.3031 - 100 S. Biscayne Blvd, Miami 
  7. Message-ID: <4isnjf$e69@news.bridge.net>
  8. References: <4iskkm$guk@cti01.citenet.net> <4isl5t$guk@cti01.citenet.net>
  9. Reply-To: michael@bridge.net
  10. NNTP-Posting-Host: ppp-mia1-43.bridge.net
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. kabir@citenet.net (larry mintz) wrote:
  14.  
  15.  
  16. >I have the following data in a file called log.dat.It represents the login/out
  17. >times of people on a network:  The fields are
  18. >                  NAME    LOGIN_TIME  LOGOUT_TIME  PORT
  19. >with the following data:
  20. >                  kthomas   3:30:45      4:00:00    23
  21. >                  urstupid  5:12:12                 67
  22. >                  iamsmart  12:34:11     13:23:11   90
  23. >                  kthomas   14:23:11     15:12:45   23
  24. >                  urstupid   5:30:12     6:00:00    25
  25.  
  26. >The structure I am using is 
  27. >          struct xx{ char NAME[10];
  28. >                     char LOGIN_TIME[10];
  29. >                     char  LOGOUT_TIME[10];
  30. >                     unsigned PORT:5;
  31. >                   };
  32. >struct xx LOGTABLE[10];     
  33.  
  34. >Question: How do I load log.dat into the struct LOGTABLE[10] so all the data 
  35. >is in the proper fields?
  36. >kabir.   
  37.  
  38.  
  39. Hi Kabir,
  40.  
  41. What works well is the  fread() function.  For example:
  42. fread(LOGTABLE, sizeof(xx), 10, FILE * stream);
  43.  the last one is a file pointer stream such as that returned by
  44. fopen().
  45.  
  46. What you have done is declared an array of 10 structures of the type
  47. xx.  if you don't know how many you need you could set up the strcture
  48. to only hold one structure at a time, put everything in a while loop
  49. that says
  50. fread();
  51. while(FILE * ! EOF)    
  52.    {
  53.    printf();        
  54.    fread();
  55.    }
  56.  
  57. Let me know if this helps.  it is easier to email me since I don't
  58. always check the newsgroups.
  59.  
  60. michael@bridge.net
  61.  
  62.  
  63.